

package com.yz.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yz.common.entity.*;
import com.yz.dao.StationRecordDao;
import com.yz.dao.StationSettingDao;
import com.yz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("sationRecordService")
public class StationRecordServiceImpl extends ServiceImpl<StationRecordDao,StationRecordEntity> implements StationRecordService {
    @Autowired
    CheckService checkService;
    @Autowired
    RecordDetailService recordDetailService;
    @Autowired
    CameraService cameraService;
    @Autowired
    ProjectService projectService;
    @Autowired
    DrawingService drawingService;

    @Override
    public long selectId() {
        return this.baseMapper.selectId();
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void postStationRecord(StationRecordForm t,UserEntity user) {
        StationRecordEntity data = t.getStationRecordEntity();
        CheckEntity check = new CheckEntity();
        check.setCreateAt(data.getUpdateAt());
        check.setCreateUser(data.getCreateUser());
        check.setName(projectService.selectById(data.getProjectId()).getName());
        check.setNameForm(contant.type_name_form[7]);
        check.setProjectId(data.getProjectId());
        check.setStatus(1);
        check.setType(5);
        check.setTypeForm(104);
        long id = 0;
        if(data.getId()==0){
            this.baseMapper.insert(data);
            id = this.baseMapper.selectId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[7]+id);
            checkService.insert(check);
        }else{
            id = data.getId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[7]+id);
            checkService.update(check,new EntityWrapper<CheckEntity>().eq("number",contant.type_all_form[4]+contant.type_detail_form[7]+id));
            recordDetailService.delete(new EntityWrapper<RecordDetailEntity>().eq("recode_id",id));
            this.baseMapper.update(data,new EntityWrapper<StationRecordEntity>().eq("id",id));
        }
        List<RecordDetailEntity> collections = t.getRecordDetailEntities();
        for (int i = 0; i < collections.size(); i++) {
            RecordDetailEntity item = collections.get(i);
            item.setRecodeId(id);
            recordDetailService.insert(item);
        }
        //图片放入数据库
        cameraService.delete(new EntityWrapper<CameraEntity>().eq("data_id",id).eq("type_form",104));
        if(t.getCameraNumber()!=null&& t.getCameraNumber().size()>0){
            for (int i = 0; i <t.getCameraNumber().size() ; i++) {
                CameraEntity item = t.getCameraNumber().get(i);
                item.setPosterId(user.getUserId());
                item.setPostTime(data.getCreateAt());
                item.setCreateUser(user.getUsername());
                item.setProjectId(data.getProjectId());
                item.setTypeWhere(2);
                item.setTypeForm(104);
                item.setDataId(id);
                item.setFiledName("0");
                item.setNumber(contant.type_all_form[4]+contant.type_detail_form[7]+id);
                cameraService.insert(item);
            }
        }
        if(t.getDrawingNumber()!=null&& t.getDrawingNumber().size()>0){
            drawingService.delete(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",104));
            for (int i = 0; i <t.getDrawingNumber().size() ; i++) {
                DrawingEntity item1 = t.getDrawingNumber().get(i);
                item1.setPosterId(user.getUserId());
                item1.setPostTime(data.getCreateAt());
                item1.setCreateUser(user.getUsername());
                item1.setProjectId(data.getProjectId());
                item1.setTypeWhere(2);
                item1.setTypeForm(104);
                item1.setDataId(id);
                item1.setNumber(contant.type_all_form[4]+contant.type_detail_form[7]+id);
                drawingService.insert(item1);
            }
        }
        if(t.getSketch()!=null&& t.getSketch().size()>0){//草图
            for (int i = 0; i <t.getSketch().size() ; i++) {
                CameraEntity item = t.getSketch().get(i);
                item.setPosterId(user.getUserId());
                item.setPostTime(data.getCreateAt());
                item.setCreateUser(user.getUsername());
                item.setProjectId(data.getProjectId());
                item.setTypeWhere(2);
                item.setTypeForm(104);
                item.setDataId(id);
                item.setFiledName("2");
                item.setNumber(contant.type_all_form[4]+contant.type_detail_form[7]+id);
                cameraService.insert(item);
            }
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public StationRecordForm getRecordDetail(long id) {
        StationRecordForm data = new StationRecordForm();
    StationRecordEntity t = new StationRecordEntity();
    t.setId(id);
        List<RecordDetailEntity> collections = recordDetailService.selectList(new EntityWrapper<RecordDetailEntity>().eq("recode_id",id));
        List<DrawingEntity> dds = drawingService.selectList(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",104));
        List<CameraEntity> cc1s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("filed_name","0").eq("data_id",id).eq("type_form",104));
        List<CameraEntity> cc2s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("filed_name","2").eq("data_id",id).eq("type_form",104));
        data.setDrawingNumber(dds);
        data.setCameraNumber(cc1s);
        data.setSketch(cc2s);
        data.setStationRecordEntity(this.baseMapper.selectById(t));
        data.setRecordDetailEntities(collections);
        return data;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteStationRecord(long id) {
        drawingService.delete(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",104));
        cameraService.delete(new EntityWrapper<CameraEntity>().eq("data_id",id).eq("type_form",104));
        this.deleteById(id);
        this.baseMapper.deleteRecord(id);
        checkService.deleteMe(5,104,id);
    }
}
